package com.psa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import com.psa.system.R;
import com.wxw.BaseLibrary.element.ElementStatus;

import java.util.List;

/**
 *
 * @author david
 * @date 2014-05-26
 */
public class ElementStatusListAdapter  extends BaseAdapter {
    private Context _context=null;
    private LayoutInflater _inflater = null;
    private List<ElementStatus> elementStatuses = null;

    public ElementStatusListAdapter(Context context, List<ElementStatus> data){
        _context = context;
        _inflater = LayoutInflater.from(_context);
        elementStatuses = data;
    }
    @Override
    public int getCount() {
        return elementStatuses==null?0:elementStatuses.size();
    }

    @Override
    public Object getItem(int i) {
        return elementStatuses==null || i<0 || i>=elementStatuses.size()?null:elementStatuses.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        HelperClass helperClass = null;
        ElementStatus elementStatus = elementStatuses.get(i);
        if (convertView == null){
            convertView = _inflater.inflate(R.layout.checkbox_item, null);
            helperClass = new HelperClass();
            helperClass.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
            helperClass.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    ((ElementStatus)compoundButton.getTag()).setChecked(b);
                }
            });
            convertView.setTag(helperClass);
        } else {
            helperClass = (HelperClass)convertView.getTag();
        }
        helperClass.checkBox.setTag(elementStatus);
        helperClass.checkBox.setText(elementStatus.getObject().toString());
        helperClass.checkBox.setChecked(elementStatus.isChecked());
        return convertView;
    }

    class HelperClass{
        public CheckBox checkBox;
    }
}